package com.ywjh.inter.servlet.doc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.DietEntity;
import com.ywjh.inter.entity.DietFileEntity;
import com.ywjh.inter.entity.DietPatientEntity;
import com.ywjh.inter.entity.DietTempEntity;
import com.ywjh.inter.service.DietService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Diet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3511191059141095137L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "add"://添加计划
			int patient_id = data.getInt("patient_id");
			int doc_id = data.getInt("doc_id");
			String tips = data.getString("tips");
			String bf_hh = data.getString("bf_hh");
			String bf_memo = data.getString("bf_memo");
			String lc_hh = data.getString("lc_hh");
			String lc_memo = data.getString("lc_memo");
			String dn_hh = data.getString("dn_hh");
			String dn_memo = data.getString("dn_memo");
			String start_time= data.getString("start_time");
			DietEntity dietEntity = new DietEntity();
			dietEntity.setAdd_doc_id(doc_id);
			dietEntity.setStart_time(StrUtil.getTimestamp(start_time));
			dietEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			dietEntity.setBf_hh(bf_hh);
			dietEntity.setBf_memo(bf_memo);
			dietEntity.setDn_hh(dn_hh);
			dietEntity.setDn_memo(dn_memo);
			dietEntity.setLc_hh(lc_hh);
			dietEntity.setLc_memo(lc_memo);
			dietEntity.setPatient_id(patient_id);
			dietEntity.setStatus(1);
			dietEntity.setTips(tips);
			DietService.getInstance().disableDieteByPatient(patient_id);
			boolean b = DietService.getInstance().addDiet(dietEntity);
			if (b) {
				r = 1;
			}
			break;
		case "add_template"://添加模版
			String name = data.getString("name");
			doc_id = data.getInt("doc_id");
			tips = data.getString("tips");
			bf_hh = data.getString("bf_hh");
			bf_memo = data.getString("bf_memo");
			lc_hh = data.getString("lc_hh");
			lc_memo = data.getString("lc_memo");
			dn_hh = data.getString("dn_hh");
			dn_memo = data.getString("dn_memo");
			DietTempEntity dietTempEntity = new DietTempEntity();
			dietTempEntity.setAdd_doc_id(doc_id);
			dietTempEntity.setAdd_time(new Timestamp(System.currentTimeMillis()));
			dietTempEntity.setBf_hh(bf_hh);
			dietTempEntity.setBf_memo(bf_memo);
			dietTempEntity.setDn_hh(dn_hh);
			dietTempEntity.setDn_memo(dn_memo);
			dietTempEntity.setLc_hh(lc_hh);
			dietTempEntity.setLc_memo(lc_memo);
			dietTempEntity.setName(name);
			dietTempEntity.setStatus(1);
			dietTempEntity.setTips(tips);
			b = DietService.getInstance().addDietTemplate(dietTempEntity);
			if (b) {
				r = 1;
			}
			break;
		case "list_template"://查询所有模版
			doc_id = data.getInt("doc_id");
			JSONArray array = new JSONArray();
			List<DietTempEntity> tempList = DietService.getInstance().listTemplateByDocId(doc_id);
			for (DietTempEntity tempEntity : tempList) {
				JSONObject tempjson = tempEntity.toJson();
				array.add(tempjson);
			}
			r = 1;
			json.put("d", array);
			break;
		case "list_diet_by_patient_id"://查看患者饮食计划
			patient_id = data.getInt("patient_id");
			doc_id = data.getInt("doc_id");
			array = new JSONArray();
			List<DietEntity> dietList = DietService.getInstance().listByPatientId(patient_id, doc_id);
			for (DietEntity entity : dietList) {
				JSONObject tempjson = entity.toJson();
				array.add(tempjson);
			}
			r = 1;
			json.put("d", array);
			break;
		case "list_diet_patient"://查看患者执行
			int diet_id=data.getInt("diet_id");
			array = new JSONArray();
			List<DietPatientEntity> diePatientList = DietService.getInstance().listDiePatientByDietId(diet_id);
			for(DietPatientEntity dietPatientEntity:diePatientList) {
				JSONObject dpjson=dietPatientEntity.toJson();
				List<DietFileEntity> filelist= DietService.getInstance().findFile(dietPatientEntity.getId());
				JSONArray filearray=new JSONArray();
				for(DietFileEntity fileEntity:filelist) {
					filearray.add(fileEntity.toJson());
				}
				dpjson.put("files",filearray);
				array.add(dpjson);
			}
			r = 1;
			json.put("d", array);
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
